package com.nulstudio.cloud.warehouse.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nulstudio.cloud.common.entity.NulWarehouse;
import com.nulstudio.cloud.common.exception.NulException;
import com.nulstudio.cloud.common.exception.NulExceptionConstants;
import com.nulstudio.cloud.common.domain.vo.WarehouseVo;
import com.nulstudio.cloud.warehouse.mapper.WarehouseMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public final class WarehouseService {

    private final Logger log = LoggerFactory.getLogger(WarehouseService.class);

    @Resource
    @NonNull
    private WarehouseMapper warehouseMapper;

    public PageInfo<WarehouseVo> fetchAll(@NonNull String name, int page, int pageSize) {
        final PageInfo<WarehouseVo> result;
        try (
                final Page<WarehouseVo> ignored = PageHelper.startPage(page, pageSize);
        ) {
            final List<WarehouseVo> warehouseList = warehouseMapper.fetchWarehouseList(name);
            result = new PageInfo<>(warehouseList);
        } catch (Exception exception) {
            log.error("Error", exception);
            throw new NulException(NulExceptionConstants.INTERNAL_SERVER_ERROR, exception.getMessage());
        }
        return result;
    }

    @NonNull
    public WarehouseVo getWarehouseById(int warehouseId) {
        final WarehouseVo warehouse = warehouseMapper.findWarehouseById(warehouseId);
        if (warehouse == null) {
            throw new NulException(NulExceptionConstants.WAREHOUSE_NOT_EXIST);
        }
        return warehouse;
    }

    public void create(@NonNull String name, @NonNull String type, @NonNull String address) {
        final NulWarehouse warehouse = new NulWarehouse(name, fetchWarehouseTypeId(type), address);
        final int result = warehouseMapper.insert(warehouse);
        if (result <= 0) {
            throw new NulException(NulExceptionConstants.INTERNAL_SERVER_ERROR);
        }
    }

    public void update(int id, @NonNull String name, @NonNull String type, @NonNull String address) {
        final NulWarehouse warehouse = new NulWarehouse(id, name, fetchWarehouseTypeId(type), address);
        final int result = warehouseMapper.update(warehouse);
        if (result <= 0) {
            throw new NulException(NulExceptionConstants.WAREHOUSE_NOT_EXIST);
        }
    }

    public void delete(int id) {
        final int result = warehouseMapper.delete(id);
        if (result <= 0) {
            throw new NulException(NulExceptionConstants.WAREHOUSE_NOT_EXIST);
        }
    }

    @NonNull
    public List<String> fetchTypeList() {
        return warehouseMapper.fetchTypeList();
    }

    private int fetchWarehouseTypeId(@NonNull String type) throws NulException {
        final Integer result = warehouseMapper.fetchTypeIdByName(type);
        if (result == null) {
            throw new NulException(NulExceptionConstants.WAREHOUSE_INVALID_TYPE);
        }
        return result;
    }
}
